package com.iot.inter.controller.newinterface.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.order.purchaseplan.request.PurchasePlanRequest;
import com.iot.inter.entity.newrequest.order.purchaseplan.response.PurchasePlanResponse;
import com.iot.inter.interfaceprocessing.newinterface.TurnToOrderService;
import com.iot.inter.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 新版：：：：
 * 去北京做：3.10 新订购套餐：2.0接口
 *
 * @author lm
 * @date 2019/3/4
 */
@RestController
@Api(description = "3.10 新订购套餐")
public class PurchaseController {

    private static Logger logger = Logger.getLogger(OrderPackageNewController.class);

    @CrossOrigin
    @RequestMapping(value = "purchasePlan", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.10 新订购套餐", response = PurchasePlanResponse.class)
    public String purchasePlan(@ApiParam @RequestBody PurchasePlanRequest re) {

        if (null != JSON.toJSONString(re)) {

            logger.info("【真正订购套餐】请求：" + JSON.toJSONString(re));
            PurchasePlanRequest pur = JSONArray.parseObject(JSON.toJSONString(re), PurchasePlanRequest.class);

            //现在去北京拿单号
            TurnToOrderService turnToOrderService = new TurnToOrderService();
            OrderPaymentManager manager = turnToOrderService.orderPlan(pur.getDeviceId(), pur.getGlobalOrder(), pur.getPackageName(), pur.getPackageCode(), pur.getPackageType(),
                    pur.getOrderPeriod(), pur.getAmount(), pur.getSecret(), pur.getPartnerCode(), pur.getPayRst(), pur.getPayType(), pur.getIsOpen(), pur.getTestFlowFlag());

            if (null == manager) {
                return ResponseUtil.responseStr("9999", "【新订套餐】接口未返回任何信息", "", "", "");
            } else {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(manager.getRetCode())) {

                    if (StringUtils.isNotBlank(manager.getOrderId())) {

                        return ResponseUtil.responseStr(manager.getRetCode(), manager.getResultInfo(), manager.getOrderId(), manager.getOrderStartDate(), manager.getOrderExpireDate());

                    } else {
                        return ResponseUtil.responseStr(manager.getRetCode(), manager.getResultInfo(), "", "", "");
                    }

                } else {
                    return ResponseUtil.responseStr(manager.getRetCode(), manager.getResultInfo(), "", "", "");
                }
            }

        } else {
            return ResponseUtil.responseStr("9998", "请求报文为空", "", "", "");
        }

    }
}
